package com.peng.leetcode;

/**
 * ReverseList
 *
 * @author: lupeng6
 * @create: 2023/3/22 16:24
 */
public class ReverseList {


    public static void main(String[] args) {
        ReverseList r = new ReverseList();
        // ListNode h = new ListNode(4, new ListNode(3, new ListNode(2, new ListNode(1))));
        ListNode h = new ListNode(4, new ListNode(3));
        ListNode reverse = r.reverse(h);
        while (reverse != null) {
            System.out.println(reverse.val);
            reverse = reverse.next;

        }
    }

    public ListNode reverse(ListNode head) {
        if (head == null) {
            return head;
        }
        ListNode prev = null;
        while (head != null) {
            ListNode temp = head.next;
            head.next = prev;
            prev = head;
            head = temp;
        }
        return prev;
    }


    public static class ListNode {
        int val;
        ListNode next;

        public ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }

        public ListNode(int val) {
            this.val = val;
        }

        public ListNode() {
        }
    }
}
